const app = getApp()

import util from "../../../utils/words";
import fUtil from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    question: '',
    flag: true, // false 未编辑  true  编辑模式
    user: '',
    userId: ''
  },



  submitHandle() {
    var url = app.globalData.url + 'ScDiscussion/addDiscussion',
      that = this,
      question = this.data.question,
      info = that.data.info;

    if (info.discussionContent == '') {
      wx.showToast({
        title: '请说点什么',
        icon: 'none'
      })
      return
    }

    if (util.filter_method(info.discussionContent).split("/")[1] == 1) {
      wx.showToast({
        title: '当前评论涉及到敏感词汇,请重新编辑',
        icon: 'none'
      })
      return
    }


    info.createTime = fUtil.formatTime1(new Date)

    wx.showLoading({
      title: '',
      icon: 'none'
    })

    app.wxRequestJson(url, info, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.showToast({
            title: '发布成功',
            icon: 'none'
          })
          question.list.push(info)
          that.setData({
            question,
            flag: false
          })
          wx.hideLoading()
        }
      },
      err => {
        console.log(err);
      })
  },
  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },

  answerHandle(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      flag: flag == 1
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    var question = JSON.parse(options.obj),
      info = {
        companyId: app.globalData.companyId,
        discussionContent: '',
        discussionParentId: question.discussionId,
        discussionType: 1, // 0 问 1 答
        isTop: 0,
        productId: question.productId,
        status: 1,
        userId: app.globalData.userId,
      },
      userId = app.globalData.userId,
      user = app.globalData.user,
      flag = question.list.length == 0;
    this.setData({
      info,
      question,
      flag,
      user,
      userId
    })

    console.log(this.data);

    console.log(fUtil.formatTime1(new Date));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})